package com.xinou.daily_report.expand.sso.mini;

import com.xinou.common.base.exception.BusException;
import com.xinou.common.util.SpringContextBeanUtil;
import com.xinou.sso.enumerate.HttpSsoStatusEnum;
import com.xinou.sso.util.ShiroRedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;

/**
 * @author lizhongyuan
 */
@Slf4j
public class MiniCredentialsMatcher implements CredentialsMatcher {

	@Override
	public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
		boolean isBlack = SpringContextBeanUtil.getBean(ShiroRedisUtil.class).isInBlackList(authenticationToken.getCredentials().toString());
		if (isBlack) {
			throw new BusException(HttpSsoStatusEnum.RE_CODE_AUTH_ERROR);
		}
		return MiniMatchUtil.checkToken(
				authenticationToken.getCredentials().toString(),
				authenticationToken.getPrincipal().toString()
		);
	}

}
